#include "ServerManager.hpp"

std::map<std::string, Server *> ServerManager::ServerMap;

Server *ServerManager::getServer(std::string name, const char *ip_addr, int port)
{
	Server *ser = new Server(name, ip_addr, port);
	ServerMap[name] = ser;	

	accept_callback *ac_cb = new accept_callback(ser);
	client_callback *cl_cb = new client_callback(ser);	
	ev::io *accept_io = new ev::io();
	struct ev_loop *loop = ev_default_loop(0); 

	//设置Server
	ser->accept_cb = ac_cb;
	ser->client_cb = cl_cb;
	ser->socket_accept = accept_io;
	ser->loop = loop;

  	//启动accept的监视器
	accept_io->set(ac_cb);
	accept_io->set(loop);
	accept_io->start(ser->fd, ev::READ);
	return ser;
}

void ServerManager::delServer(std::string name)
{
	delete ServerMap[name]->client_cb;
	delete ServerMap[name]->accept_cb;
	delete ServerMap[name]->socket_accept;
	delete ServerMap[name];
	ServerMap[name] = NULL;
	ServerMap.erase(name);
}
